<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generateds a Cairngorm command for a given table
 */
function generateAppModel( $tables ) {

	$export =
'package '.con::$package.'.model
{

	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */

	import mx.collections.ListCollectionView;
	import com.crazedcoders.lib.structure.TypedArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField

';
	$properties = '';
	$imports = '';
	$inits = '';
	foreach( $tables as $tableName=>$table ) {
		if( ! array_key_exists( 'isManyToMany', $table )
				|| $table['isManyToMany']
			){
			continue;
		}

		//$tableName = $tables[$k][0]['table_name'];
		$className = toCamelCase( $tableName );
		$inits .=
'	   		'.toCamelCaseMember($className)
			.'s = new TypedArrayCollection( '.$className.' );
';
		$pkey = getPKey( $table );
		if( $pkey != "" ) {
			$inits .=
'			sort = new Sort();
			sort.fields = [ new SortField( "'.toCamelCaseMember($pkey).'",
					false, false, true ) ];
			'.toCamelCaseMember($className).'s.sort = sort;
';
		}
		$properties .=
'		public var '.toCamelCaseMember($className)
			.'s:TypedArrayCollection = null;
';
		$properties .=
'		public var '.toCamelCaseMember($className)
			.'Count:int = -1;
';
		$imports.=
'	import '.con::$package.'.vo.'.($className).'
';
	}
;
	$export .= $imports .
'

	[Bindable]
	public class AppModel
	{
		private static var _instance:AppModel;

		public static function getInstance():AppModel
		{
			if (_instance == null)
			{
				_instance = new AppModel();
			}
			return _instance;
		}

		public function AppModel()
		{
			if (_instance != null)
	   		{
				throw new
					Error("Only one Model instance should be instantiated!");
				return;
	   		}
			var sort:Sort;
'. $inits .
'		}
'
	.$properties.
'	}
}
';

	return $export;

}


?>
